<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 120px;
  height: 120px;
  line-height: 120px;
  text-align: center;
  border: red;
}

.avatar {
  width: 120px;
  height: 120px;
  display: block;
}
</style>
<template>
  <div>
    <el-form ref="addForm" :model="addNewForm" :rules="addNewRules" label-width="150px"
             style="width: 1000px;position:relative;overflow: hidden;height: 650px">
      <el-form-item label="管理员账号" prop="username">
        <el-input v-model="addNewForm.username"/>
      </el-form-item>
      <el-form-item label="管理员密码" prop="password">
        <el-input type="password" v-model="addNewForm.password"/>
      </el-form-item>
      <el-form-item label="昵称" prop="nickname">
        <el-input v-model="addNewForm.nickname"/>
      </el-form-item>
      <el-form-item label="头像" prop="icon">
        <el-upload
            class="avatar-uploader"
            action="http://localhost:9999/zx-resource/resources/upload/image/product"
            :show-file-list="false"
            :on-success="handleAvatarSuccess">
          <img v-if="addNewForm.icon" :src="addNewForm.icon" class="avatar" alt="404">
          <i v-else class="el-icon-plus avatar-uploader-icon"/>
        </el-upload>
      </el-form-item>
      <el-form-item label="电话号码" prop="phone">
        <el-input v-model="addNewForm.phone"/>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="addNewForm.email"/>
      </el-form-item>
      <el-form-item label="序号" prop="sort" style="display: inline-block;vertical-align: top">
        <el-input v-model="addNewForm.sort" style="width: 100px;"/>
      </el-form-item>
      <el-form-item label="禁用/启用" prop="isEnable" style="display: inline-block;vertical-align: top">
        <el-switch v-model="addNewForm.isEnable" :active-value="1" :inactive-value="0"/>
      </el-form-item>
      <el-form-item label="角色" prop="roleIds" style="display: inline-block">
        <el-select v-model="addNewForm.roleIds" multiple collapse-tags placeholder="请选择">
          <el-option v-for="item in roleTable" :key="item.id" :label="item.name" :value="item.id"/>
        </el-select>
      </el-form-item>
      <el-form-item style="position: absolute;left: 50%;bottom: 0;transform: translateX(-71%)">
        <el-button type="primary" @click="submitForm">添加</el-button>
        <el-button type="danger" @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>

export default {
  name: "managerAddNew",
  data() {
    return {
      addNewForm: {
        username: "",
        password: "",
        icon: "",
        nickname: "",
        phone: "",
        email: "",
        isEnable: 0,
        sort: "",
        roleIds: [],
      },
      addNewRules: {
        username: [
          {required: true, message: "管理员账号不可为空", trigger: "blur"},
          {max: 20, message: "管理员账号字符数量不可大于20个", trigger: "blur"}
        ],
        password: [
          {required: true, message: "密码不可为空", trigger: "blur"}
        ],
        icon: [],
        nickname: [
          {required: true, message: "昵称不可为空", trigger: "blur"}
        ],
        phone: [
          {required: true, message: "电话号码不可为空", trigger: "blur"},
          {
            pattern: /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur"
          }
        ],
        email: [
          {pattern: /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/, message: "请输入正确的邮箱", trigger: "blur"}
        ],
        isEnable: [],
        sort: [
          {required: true, message: "序号不可为空", trigger: "blur"}
        ],
        roleIds: [
          {required: true, message: "角色不可为空", trigger: "blur"}
        ],
      },
      roleTable: [],
    }
  },
  methods: {
    handleAvatarSuccess(response) {
      this.addNewForm.icon = response.data.url
    },
    submitForm() {
      this.$refs.addForm.validate((valid) => {
        if (valid) {
          let url = "http://localhost:9999/zx-ams/admin/add-new"
          let formData = this.qs.stringify(this.addNewForm);
          this.axios.create(
              {'headers': {'Authorization': localStorage.getItem('adminJwt')}}
          ).post(url, formData).then((response) => {
            if (response.data.state === 200) {
              this.$message({message: "数据添加完毕", type: "success"});
              this.resetForm();
              this.$emit("changeTab", "adminList");
            } else {
              this.$message({message: response.data.message, type: "error"})
            }
          })
        } else {
          this.$message({message: "数据尚未符合要求", type: "error"})
        }
      });
    },
    resetForm() {
      this.$refs.addForm.resetFields();
      this.addNewForm.isEnable = 0;
    },//用于清除el-form内所有内容
    loadRole() {
      let url = "http://localhost:9999/zx-ams/role/list"
      this.axios.create(
          {'headers': {'Authorization': localStorage.getItem('adminJwt')}}
      ).get(url).then((response) => {
        let jsonResult = response.data
        if (jsonResult.state === 200) {
          jsonResult = jsonResult.data;
          jsonResult = jsonResult.filter(item => item.name !== "超级管理员")
          this.roleTable = jsonResult;
        } else {
          this.$message({message: response.data.message, type: "error"})
        }
      })
    }
  },
  mounted() {
    this.loadRole();
  }
}
</script>